package com.example.houtai.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.houtai.entity.SysDept;
import com.example.houtai.service.ISysDeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author 龚梦
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {

    //luweijia

    @Autowired
    private ISysDeptService iSysDeptService;
    //部门列表
    @RequestMapping("/list")
    public PageInfo<SysDept> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        System.out.println(pageNum+""+pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<SysDept> list =  iSysDeptService.listVO();
        PageInfo info = new PageInfo(list);
        return info;
    }

    //普通列表
    @RequestMapping("/pList")
    public List<SysDept> pList(){

        return iSysDeptService.listVO();
    }
    //部门删除
    @RequestMapping("/delete")
    public boolean delete(Integer[] ids){
        boolean b = false;
        for(Integer id : ids ){
            System.out.println(id+"==============");
            b = iSysDeptService.removeById(id);
        }
        return b;
    }

    //添加上级部门
    @RequestMapping("/addParentId")
    public List<SysDept> addParentId(Integer id){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("parent_id",id);
        List list = iSysDeptService.list(qw);
        return list;
    }

    //添加和修改操作
    @RequestMapping("/addEndUpdate")
    public boolean addEndUpdate(@RequestBody SysDept sysDept){
        Long deptId = sysDept.getDeptId();
        if (deptId != null){
            return iSysDeptService.updateById(sysDept);
        }
        return iSysDeptService.save(sysDept);
    }
}
